package com.lais.record.base.service;

import com.fasterxml.jackson.databind.ser.Serializers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lais.record.base.constant.BaseConstant;
import com.lais.record.base.constant.RecordBaseEnum;
import com.lais.record.base.mapper.AccountMapper;
import com.lais.record.base.pojo.Account;
import com.lais.record.common.constant.RecordEnum;
import com.lais.record.common.utils.UserInfoUtil;
import com.lais.record.common.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * @Author: luowen
 * @Description:
 * @Date: Create in 21:58 2019/11/27
 */
@Service
@Transactional
@SuppressWarnings("all")
@Slf4j
public class AccountService extends BaseService{

    @Autowired
    private AccountMapper accountMapper;

    /**
     * 账户添加
     * @param account
     */
    public void save(Account account) {
        log.info("accountService save...");
        account.setStatus(BaseConstant.AccountStatus.ENABLE); //初始为有效
        account.setCreated(new Date());
        account.setId(String.valueOf(idWorker.nextId()));
        accountMapper.insert(account);
    }

    public void update(Account account) {
        log.info("accountService upate...");
        account.setUpdated(new Date());
        Example example = new Example(Account.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uid", UserInfoUtil.getUserId());
        criteria.andEqualTo("id",account.getId());
        accountMapper.updateByExampleSelective(account,example);
    }

    public void deleteByIds(List<String> ids) {
        log.info("accountService deleteByIds...");
        String uid = UserInfoUtil.getUserId();
        accountMapper.updateStatusByIds(ids,uid);
    }

    /**
     * 根据id查询账户信息
     * @param id
     * @return
     */
    public Account getById(String id) {
        log.info("accountService getById.....");
        Example example = new Example(Account.class);
        example.createCriteria().andEqualTo("uid",UserInfoUtil.getUserId()).andEqualTo("id",id).andEqualTo("status",BaseConstant.AccountStatus.ENABLE);
        List<Account> accounts = this.accountMapper.selectByExample(example);
        if(accounts == null || accounts.size()==0){
            return null;
        }
        return accounts.get(0);
    }

    /**
     * 查询登录用户下的所有账户信息
     * @return
     */
    public List<Account> findAll() {
        log.info("accountService findAll.....");
        Example example = new Example(Account.class);
        example.createCriteria().andEqualTo("uid",UserInfoUtil.getUserId()).andEqualTo("status", BaseConstant.AccountStatus.ENABLE);
        List<Account> accounts = this.accountMapper.selectByExample(example);
        return accounts;
    }

    /**
     * 分页条件查询
     * @param page
     * @param pageSize
     * @param account
     * @return
     */
    public PageResult search(Integer page, Integer pageSize, Account account) {
        log.info("accountService search.....");
        PageHelper.startPage(page,pageSize);
        account.setUid(UserInfoUtil.getUserId());
        Page<Account> accounts = (Page<Account>) accountMapper.select(account);
        PageResult pageResult = new PageResult();
        if(accounts==null || accounts.size()==0){
            //说明该用户不存在账户信息
            pageResult.setCode(RecordBaseEnum.ACCOUNT_NOT_EXIST.getCode());
            pageResult.setMessage(RecordBaseEnum.ACCOUNT_NOT_EXIST.getMessage());
            pageResult.setFlag(false);
        }else{
            pageResult.setCode(RecordEnum.SUCCESS.getCode());
            pageResult.setMessage(RecordEnum.SUCCESS.getMessage());
            pageResult.setFlag(true);
            pageResult.setPage(page);
            pageResult.setPageSize(pageSize);
            pageResult.setTotal(accounts.getTotal());
            pageResult.setTotalPage(accounts.getPages());
        }
        return pageResult;
    }
}
